﻿using System.IO;
using System;
using System.Collections.Generic;

using System.Text;

namespace xReplace
{
    class ExcludeCommand : AbstractProcessingCommand
    {
        public ExcludeCommand(ref Content pipes)
            : base(ref pipes)
        {
        }

        public override bool ProcessCommand(System.Xml.XmlNodeList param)
        {
            string fromLabel = GetNodeValue("fromlabel", param);
            string src = GetNodeValue("match", param);
            string startsWith = GetNodeValue("startswith", param);
            string endsWith = GetNodeValue("endswith", param);
            if (GetNodeAttributeAsBoolean("startswith", "escape", param))
                startsWith = SpecialCharHelper.Unescape(startsWith);
            if (GetNodeAttributeAsBoolean("endswith", "escape", param))
                endsWith = SpecialCharHelper.Unescape(endsWith);
            if (GetNodeAttributeAsBoolean("src", "escape", param))
                src = SpecialCharHelper.Unescape(src);

            string ignoreCaseStr = GetNodeValue("ignorecase", param);
            bool ignoreCase = (ignoreCaseStr.ToLower() == "true") ? true : false;

            fromLabel = (fromLabel == "") ? Content.DEFAULT_KEY : fromLabel;
            string tmp = contentPipelines.LoadContent(fromLabel);
            TextFragment frag = new TextFragment(tmp);
            if (frag.GrabFragment(startsWith, endsWith, ignoreCase)) return true;
            return false;
        }
    }
}
